<template>
    <div>
        <div v-if="pageFlag == 0" class="exchangerecord">
            <div class="title">
                {{ $t('assemble.商品兑换记录') }}
            </div>
            <div class="exchangerecordbox">

                <div class="databox">
                    <div class="dataHeader">
                        <!-- <p> {{ $t('assemble.商品图片') }}</p> -->
                        <p> {{ $t('assemble.商品名称') }}</p>
                        <p> {{ $t('assemble.兑换金额') }}</p>
                        <p>{{ $t('assemble.比例金额') }} </p>
                        <p> {{ $t('assemble.状态') }}</p>
                        <p> {{ $t('assemble.类型') }}</p>
                        <p> {{ $t('assemble.兑换时间') }}</p>
                    </div>
                    <div class="dataList">
                        <div class="item" v-for="(item, index) in dataList" :key="item.id">
                            <!-- <img src="../../../static/image/logo.png" alt=""> -->
                            <p style="width: 150px; text-align: center;">{{ item.goods.name }}</p>
                            <p style="width: 150px; text-align: center;">{{ item.goods.price }}</p>
                            <p style="width: 150px; text-align: center;">{{ item.order_price_proportion }}</p>
                            <p v-if="item.status == 1" style="width: 100px; text-align: center;">{{ $t('assemble.已使用') }}
                            </p>
                            <p v-if="item.status == 0" style="width: 100px; text-align: center;">{{ $t('assemble.未使用') }}
                            </p>
                            <p v-if="item.type == 1" style="width: 100px; text-align: center;">{{ $t('assemble.积分') }}</p>
                            <p v-if="item.type == 2" style="width: 100px; text-align: center;">{{ $t('assemble.代金券') }}</p>

                            <p style="width: 200px; text-align: center;">{{ item.created_at }}</p>
                        </div>
                    </div>
                </div>
                <div class="fenye">
                    <el-pagination background layout="prev, pager, next" :page-size="this.pageInfo.pageSize"
                        :page-sizes="[5, 10, 15, 20]" :total="pageInfo.total" @size-change="handleSizeChange"
                        @current-change="handleCurrentChange">
                    </el-pagination>
                </div>
            </div>
        </div>
        <div v-if="pageFlag == 1" class="exchangerecord">
            <div class="title">
                {{ $t('assemble.现金兑换') }}
            </div>
            <div class="exchangerecordbox xianjiBox">
                <div class="duihuanBox">
                    <el-form ref="duihuanform" :model="duihuanform" label-width="230px">
                        <el-form-item :label="$t('assemble.转账方式') + ':'">
                            <el-select v-model="duihuanform.mode" placeholder="请选择转账方式">
                                <!-- <el-option label="EMT转账" value="EMT转账"></el-option> -->
                                <el-option label="支票" value="支票"></el-option>
                            </el-select>
                        </el-form-item>
                        <!-- label="兑换金额(Exchange amount):"  -->
                        <el-form-item :label="$t('assemble.兑换金额') + ':'">
                            <el-input v-model="duihuanform.amount"></el-input>
                        </el-form-item>
                        <!-- <el-form-item label="真实金额:">
                            <el-input v-model="duihuanform.realamount"></el-input>
                        </el-form-item> -->
                        <el-form-item :label="$t('assemble.真实姓名') + ':'">
                            <el-input v-model="duihuanform.realname"></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('assemble.邮箱地址') + ':'">
                            <el-input v-model="duihuanform.email"></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('assemble.电话号码') + ':'">
                            <el-input v-model="duihuanform.phone"></el-input>
                        </el-form-item>
                    </el-form>
                </div>
                <div class="btnbox">
                    <div class="btn ok" @click="CashVisible = true">{{ $t('assemble.确定') }}</div>
                    <div class="btn close" @click="back()">{{ $t('assemble.取消') }}</div>
                </div>

            </div>

        </div>
        <div v-if="pageFlag == 2" class="exchangerecord">
            <div class="title">
                {{ $t('assemble.代金券兑换') }}
            </div>
            <div class="exchangerecordbox">

                <div class="quanbox">
                    <div class="desc">
                        {{ $t('assemble.描述') }}
                    </div>
                    <div class="shengyu">
                        <span>{{ $t('assemble.剩余积分') }}</span><span style="margin-left: 20px;">{{ money }}</span>
                    </div>
                    <div class="duihuantext">
                        {{ $t('assemble.可兑换代金券') }}
                    </div>

                    <div class="searchBox">
                        <input class="searchinput" v-model="searchValue" type="text" placeholder="关键词搜索(Keyword search)">
                        <div class="searchBtn" @click="getcoupon">
                            {{ $t('assemble.搜索') }}
                        </div>
                    </div>

                    <div class="quanDataList">
                        <div class="dataitem" v-for="(item, index) in couponData" @click="handleSelectcoupon(index)">
                            <img src="../../../static/image/quanimg.png" alt="">
                            <div class="quantext">{{ item.name }}</div>
                            <div class="suoxujifen">{{ item.price }}</div>
                        </div>

                    </div>

                    <div class="detailBox">
                        <div class="detailLeft">
                            <div class="leftItem" v-for="(item, index) in selectCoupon">
                                <img src="../../../static/image/quanimg.png" alt="">
                                <div class="quantext">{{ item.name }}</div>
                                <div class="suoxujifen">{{ item.price }}</div>
                            </div>

                        </div>


                        <div class="detailRigth">
                            <div class="jian">{{ selectmoney }}</div>
                            <div class="shengyus">{{ $t('assemble.剩余积分') }} {{ money }}</div>
                            <div class="lijigoumai" @click="handlepay">{{ $t('assemble.购买') }}</div>

                        </div>
                    </div>
                </div>
            </div>

        </div>
        <el-dialog title="提示(Tips)" :visible.sync="CouponVisible" width="30%" center>
            <div class="deltext">{{ $t('assemble.确定兑换代金券') }}</div>
            <div slot="footer" class="dialog-footer">
                <div class="btnitem ok" @click="handleduihuanCoupon">{{ $t('assemble.确定') }}</div>
                <div class="btnitem close" @click="CouponVisible = false">{{ $t('assemble.取消') }}</div>
            </div>
        </el-dialog>

        <el-dialog title="提示" :visible.sync="CashVisible" width="30%" center>
            <div class="deltext" style="width:135px">您确定要兑换吗?</div>
            <div slot="footer" class="dialog-footer">
                <div class="btnitem ok" @click="handleduihuanCash">确定</div>
                <div class="btnitem close" @click="CashVisible = false">取消</div>
            </div>
        </el-dialog>

    </div>
</template>
    
<script>
import { coupon, userOwn, couponExchange, cashExchange, orderList } from "@/utils/api"
export default {
    data() {
        return {
            pageInfo: {
                total: 0,
                page: 1,
                pageSize: 5
            },
            CashVisible: false,
            CouponVisible: false,
            pageFlag: 0,
            dataList: [
                {
                    id: 1,
                    name: "商品名称1",
                    price: "333",
                    date: '2022.09.09',
                    status: "1"
                },
                {
                    id: 2,
                    name: "商品名称2",
                    price: "444",
                    date: '2022.09.09',
                    status: "0"

                }
            ],
            duihuanform: {
                mode: "",//转账方式
                amount: "",//兑换金额
                // realamount: "",//真实金额
                realname: "",//真实姓名
                email: "",
                phone: "",//电话号码
            },
            searchValue: "",
            couponData: [],
            selectCoupon: [],
            money: "",
            selectmoney: 0,
            modleTiele: this.$t('assemble.提示')
        }
    },
    created() {
        this.getcoupon()
        this.getuserOwn()
        this.getorderList()
        console.log("duihuanType", this.$route.query.duihuanType)
        if (this.$route.query.duihuanType == 'xianjin') {
            this.pageFlag = 1
        }
        if (this.$route.query.duihuanType == 'quan') {
            this.pageFlag = 2
        }

    },
    methods: {

        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.pageInfo.page = val
            this.getorderList()
        },
        async getorderList() {
            let res = await orderList({
                token: localStorage.getItem("token"),
                page: this.pageInfo.page,
                page_size: this.pageInfo.pageSize
            })
            console.log("商品兑换记录", res)
            this.dataList = res.data.data
            this.pageInfo.total = res.data.total
        },
        async getuserOwn() {
            let res = await userOwn({
                token: localStorage.getItem("token")
            })
            console.log("用户信息", res)
            this.money = res.data.money
        },
        async getcoupon() {
            let res = await coupon({
                token: localStorage.getItem("token"),
                name: this.searchValue
            })
            console.log("代金券列表", res)
            this.couponData = res.data
            this.couponData.forEach((item, index) => {
                item.check = false
            })
        },
        handleSelectcoupon(index) {
            this.selectCoupon = []
            this.selectmoney = 0
            this.couponData[index].check = !this.couponData[index].check
            this.$forceUpdate()
            this.couponData.forEach((item, index) => {
                if (item.check) {
                    this.selectCoupon.push(item)
                    this.selectmoney += item.price * 1
                }
            })
        },
        handlepay() {
            if (this.selectCoupon.length == 0) {
                this.$message({
                    message: '请先选择兑换优惠券！',
                    type: 'warning'
                });
            } else {
                this.CouponVisible = true
            }

        },
        async handleduihuanCash() {
            let data = {
                token: localStorage.getItem('token'),
                change_num: this.duihuanform.amount,
                name: this.duihuanform.realname,
                email: this.duihuanform.email,
                mobile: this.duihuanform.phone,
                type: this.duihuanform.mode == 'EMT转账' ? 1 : 2
            }
            console.log("data", data)
            // return
            let res = await cashExchange(data)
            if (res.code == 1000) {
                console.log("现金兑换", res)
                this.CashVisible = false
                this.$message({
                    message: "兑换成功！",
                    type: 'success'
                });
                let that = this
                setTimeout(() => {
                    that.$router.go(0)
                }, 1000)
            } else {
                this.$message({
                    message: res.message,
                    type: 'warning'
                });
            }
        },
        async handleduihuanCoupon() {
            let ids = ""
            this.selectCoupon.forEach((item, index) => {
                if (item.check) {
                    ids += item.id + ","
                }
            })
            console.log("idsssssssssss", ids)
            let res = await couponExchange({
                token: localStorage.getItem("token"),
                ids: ids
            })
            if (res.code == 1000) {
                this.CouponVisible = false
                this.$message({
                    message: res.message,
                    type: 'success'
                });
                let that = this
                setTimeout(() => {
                    that.$router.go(0)
                })
            } else {
                this.$message({
                    message: res.message,
                    type: 'warning'
                });
            }
        },
        back() {
            this.$router.go(-1)
        }
    }
}
</script>
    
<style scoped lang="scss">
.deltext {
    width: 165px;
    height: 19px;
    font-size: 13px;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
    color: #000000;
    line-height: 19px;
    margin: 0 auto;
}

.dialog-footer {
    width: 394px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;

    .btnitem {
        width: 132px;
        height: 50px;
        border-radius: 10px 10px 10px 10px;
        opacity: 1;
        font-size: 18px;
        font-family: PingFang SC-Bold, PingFang SC;
        font-weight: bold;
        color: #333333;
        line-height: 50px;
        text-align: center;
        cursor: pointer;
    }

    .ok {
        background: #0057A7;
        color: #ffffff;
    }

    .close {
        border: 1px solid #CCCCCC;
        color: black;
    }
}

.exchangerecord {
    .title {
        font-size: 15px;
        font-family: PingFang SC-Bold, PingFang SC;
        font-weight: bold;
        color: #333333;
        line-height: 18px;
    }

    .exchangerecordbox {
        padding: 30px;
        padding-bottom: 75px;
        background: #ffffff;
        margin-top: 22.5px;

        .databox {
            margin-top: 15px;

            .dataHeader {
                width: 1095px;
                height: 79px;
                display: flex;
                justify-content: space-between;

                p {
                    font-size: 13px;
                    font-family: PingFang SC-Bold, PingFang SC;
                    font-weight: bold;
                    color: #333333;
                    line-height: 18px;

                    &:nth-of-type(1) {
                        width: 150px;
                        text-align: center;
                    }

                    &:nth-of-type(2) {
                        width: 150px;
                        text-align: center;
                    }

                    &:nth-of-type(3) {
                        width: 140px;
                        text-align: center;
                    }

                    &:nth-of-type(4) {
                        width: 100px;
                        text-align: center;
                    }

                    &:nth-of-type(5) {
                        width: 100px;
                        text-align: center;
                    }

                    &:nth-of-type(6) {
                        width: 200px;
                        text-align: center;
                    }

                    // &:nth-of-type(7) {
                    //     width: 200px;
                    //     text-align: center;
                    // }
                }
            }

            .dataList {
                font-size: 15px;
                font-family: PingFang SC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                line-height: 18px;

                .item {
                    width: 100%;
                    height: 106px;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    border-bottom: 2px solid #F7F8FA;
                    padding-left: 10px;

                    img {
                        width: 90px;
                        height: 75px;
                        border-radius: 10px 10px 10px 10px;
                        opacity: 1;
                    }
                }
            }

        }
    }

    .xianjiBox {
        padding: 40px 200px;

        .duihuanBox {
            width: 1070px;
        }

        .btnbox {
            display: flex;
            margin-left: 342px;

            .btn {
                width: 132px;
                height: 50px;
                border-radius: 10px 10px 10px 10px;
                font-size: 18px;
                font-family: PingFang SC-Bold, PingFang SC;
                font-weight: bold;
                line-height: 50px;
                text-align: center;
                cursor: pointer;
            }

            .ok {
                background: #0057A7;
                color: #ffffff;
            }

            .close {
                border: 1px solid #CCCCCC;
                color: black;
                margin-left: 173px;
            }
        }
    }

    .quanbox {
        width: 956px;
        // height: 300px;
        // background-color: pink;
        margin: 0 auto;

        .desc {
            font-size: 13px;
            font-family: PingFang SC-Regular, PingFang SC;
            font-weight: 400;
            color: #AAAAAA;
            line-height: 18px;
        }

        .shengyu {
            font-size: 15px;
            font-family: PingFang SC-Bold, PingFang SC;
            font-weight: bold;
            color: #333333;
            line-height: 18px;
            margin-top: 30px;
        }

        .duihuantext {
            font-size: 13px;
            font-family: PingFang SC-Regular, PingFang SC;
            font-weight: 400;
            color: #333333;
            line-height: 18px;
            margin-top: 30px;

        }

        .searchBox {
            width: 80%;
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 30px;

            .searchinput {
                width: 600px;
                height: 37px;
                background: #F7F8FA;
                border-radius: 10px 10px 10px 10px;
                opacity: 1;
                border: none;
                text-indent: 20px;
                outline: none;
            }

            .searchBtn {
                width: 123px;
                height: 37px;
                background: #0057A7;
                border-radius: 10px 10px 10px 10px;
                opacity: 1;
                border: 1px solid #0057A7;
                font-size: 13px;
                font-family: PingFang SC-Bold, PingFang SC;
                font-weight: bold;
                color: #FFFFFF;
                line-height: 37px;
                text-align: center;
                cursor: pointer;
            }
        }

        .quanDataList {
            width: 717px;
            height: 327px;
            background: #F7F8FA;
            border-radius: 20px 20px 20px 20px;
            margin-top: 30px;
            padding: 40px 60px;
            box-sizing: border-box;
            display: flex;
            // justify-content: space-between;
            flex-wrap: wrap;

            .dataitem {
                width: 120px;
                height: 120px;
                text-align: center;
                margin-bottom: 40px;
                cursor: pointer;

                img {
                    width: 60px;
                    height: 60px;
                    cursor: pointer;

                }

                .quantext {
                    width: 100%;
                    font-size: 20px;
                    font-family: PingFang SC-Bold, PingFang SC;
                    font-weight: bold;
                    color: #000000;
                    text-align: center;
                    cursor: pointer;

                }

                .suoxujifen {
                    width: 100%;
                    text-align: center;
                    font-size: 13px;
                    font-family: PingFang SC-Regular, PingFang SC;
                    font-weight: 400;
                    color: #D32526;
                    cursor: pointer;

                }
            }
        }

        .detailBox {
            width: 100%;
            height: 135px;
            margin-top: 40px;
            padding-left: 60px;
            box-sizing: border-box;
            display: flex;
            justify-content: space-between;

            .detailLeft {
                width: 600px;
                height: 150px;
                display: flex;

                .leftItem {
                    width: 75px;
                    height: 150px;
                    display: flex;
                    flex-direction: column;
                    margin-left: 20px;
                    // justify-content: space-between;

                    img {
                        width: 75px;
                        height: 75px;
                    }

                    .quantext {
                        width: 100%;
                        font-size: 15px;
                        font-family: PingFang SC-Bold, PingFang SC;
                        font-weight: bold;
                        color: #000000;
                        line-height: 18px;
                        text-align: center;
                    }

                    .suoxujifen {
                        width: 100%;
                        font-size: 13px;
                        font-family: PingFang SC-Regular, PingFang SC;
                        font-weight: 400;
                        color: #333333;
                        line-height: 15px;
                        text-align: center;
                    }
                }

            }

            .detailRigth {
                height: 150px;
                display: flex;
                flex-direction: column;
                justify-content: space-between;

                .jian {
                    font-size: 15px;
                    font-family: PingFang SC-Bold, PingFang SC;
                    font-weight: bold;
                    color: #D32526;
                    text-align: right;
                    // margin-top: 60px;
                }

                .shengyus {
                    font-size: 13px;
                    font-family: PingFang SC-Regular, PingFang SC;
                    font-weight: 400;
                    color: #333333;
                    text-align: right;
                }

                .lijigoumai {
                    width: 99px;
                    height: 37px;
                    background: #0057A7;
                    border-radius: 10px 10px 10px 10px;
                    opacity: 1;
                    font-size: 15px;
                    font-family: PingFang SC-Bold, PingFang SC;
                    font-weight: bold;
                    color: #FFFFFF;
                    line-height: 37px;
                    text-align: center;
                    cursor: pointer;
                }
            }
        }


    }
}

.fenye {
    margin-top: 50px;
    float: right;

    ::v-deep .el-pagination.is-background .el-pager li:not(.disabled).active {
        background-color: #0057A7;
    }
}

::v-deep .el-input__inner {
    background: #F7F8FA;
    border: 1px solid #F7F8FA;
    border-radius: 10px;
    width: 717px;
    height: 50px;
}</style>